package com.qkl.xiatang.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.dto.pond.AddPondGroupDTO;
import com.qkl.xiatang.app.dto.pond.CountPondAndGruopDetail;
import com.qkl.xiatang.app.dto.pond.PondGroupDTO;
import com.qkl.xiatang.app.dto.pond.PondListOutDTO;
import com.qkl.xiatang.app.entity.CompanyEntity;
import com.qkl.xiatang.app.entity.user.AppUserInfo;
import com.qkl.xiatang.app.entity.pond.PondEntity;
import com.qkl.xiatang.app.entity.pond.PondGroup;
import com.qkl.xiatang.app.entity.pond.PondGroupDetail;
import com.qkl.xiatang.app.exception.BusinessException;
import com.qkl.xiatang.app.mapper.CompanyMapper;
import com.qkl.xiatang.app.mapper.PondGroupDetailMapper;
import com.qkl.xiatang.app.mapper.PondGroupMapper;
import com.qkl.xiatang.app.mapper.PondManagerMapper;
import com.qkl.xiatang.app.utils.AppSessionUtils;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.appenum.PondGroupTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PondManagerService {

    @Autowired
    private PondManagerMapper pondMapper;

    @Autowired
    private PondGroupMapper pondGroupMapper;

    @Autowired
    private PondGroupDetailMapper pondGroupDetailMapper;

    @Autowired
    private CompanyMapper companyMapper;

    /**
     *  查询塘(棚)列表
     * @param companyId
     * @return
     */
    public  List<PondListOutDTO> queryPondList(String companyId,String pondGroupId,Integer type){
        return pondMapper.queryPondList(companyId,pondGroupId,type);
    }

    public BusinessDTO<Object> queryPondList(String pondGroupId,Integer type){
        if(StringUtils.isNotEmpty(pondGroupId)){
            PondGroup queryPondGroup = pondGroupMapper.selectById(pondGroupId);
            if(null != queryPondGroup){
                type = queryPondGroup.getType();
            }
        }else if(null == type){
            type = PondGroupTypeEnum.FEED.getType();
        }
        String companyId = AppSessionUtils.getCompanyId();
        Map<String,Object> map = new HashMap<>();
        map.put("list",this.queryPondList(companyId,pondGroupId,type));
        return BusinessDTO.sucessBusiness(map,"查询塘(棚)列表");
    }

    /**
     * 查询塘(棚)分组列表
     * @param companyId
     * @return
     */
    public  List<Map<String,Object>> queryPondGroupList(String companyId,Integer type){
        return pondMapper.queryPondGroupList(companyId,type);
    }

    public BusinessDTO<Object> queryPondGroupList(Integer type){
        if(null == type){
            type = PondGroupTypeEnum.FEED.getType();
        }
        String companyId = AppSessionUtils.getCompanyId();
        Map<String,Object> map = new HashMap<>();
        map.put("list",this.queryPondGroupList(companyId,type));
        return BusinessDTO.sucessBusiness(map,"查询塘(棚)分组列表");
    }


    public String addPondGroup(PondGroup pondGroup){
        AppUserInfo user = AppSessionUtils.getSessionUser();
        if(StringUtils.isEmpty(pondGroup.getCompanyId()) && null != user){
            pondGroup.setCompanyId(user.getCompanyId());
        }
        pondGroup.setId(AppUtils.genIdStr(pondGroup));
        AppUtils.setEntityInfo(pondGroup);
        pondGroupMapper.insert(pondGroup);
        return pondGroup.getId();
    }

    public void addPondGroupDetail(String pondGroupId,List<String> pondIdList){
        if(ObjectUtils.isEmpty(pondIdList)){
            return;
        }
        List<PondGroupDetail> detailList = new ArrayList<>();
        for(String pindid : pondIdList){
            PondGroupDetail detail = new PondGroupDetail();
            detail.setId(AppUtils.genIdStr(detail));
            detail.setGroupId(pondGroupId);
            detail.setCompanyId(AppSessionUtils.getCompanyId());
            detail.setPondId(pindid);
            AppUtils.setEntityInfo(detail);

            detailList.add(detail);
        }
        pondMapper.addPondGroupDetail(detailList);
    }

    public Integer deletePondGruopDetail(String pondGroupId,List<String> pondIdList,Integer type){
        return pondMapper.deletePondGruopDetail(pondGroupId,pondIdList,type,AppSessionUtils.getCompanyId());
    }

    public Integer deletePondGruopDetail(String pondGroupId,Integer type){
        return this.deletePondGruopDetail(pondGroupId,null,type);
    }

    public Integer deletePondGruopDetail(List<String> pondIdList,Integer type){
        if(ObjectUtils.isEmpty(pondIdList)){
            return 0;
        }
        return this.deletePondGruopDetail("--",pondIdList,type);
    }

    /**
     * 添加 塘分组
     * @param webDTO
     * @return
     */
    @Transactional
    public BusinessDTO<Object> addPondGroup(AddPondGroupDTO webDTO){
        String groupName = webDTO.getGroupName();
        List<String> pondIdList = webDTO.getPondIdList();
        Integer type = webDTO.getType();
        if(null == type){
            type = PondGroupTypeEnum.FEED.getType();
        }
        BusinessDTO<Object> check = this.checkPondGroup(pondIdList,type);
        if( !check.isSucess()){
            return check;
        }
        PondGroup pondGroup = new PondGroup();
        pondGroup.setGroupName(groupName);
        pondGroup.setType(type);
        String pondGroupId = this.addPondGroup(pondGroup);
        this.addPondGroupDetail(pondGroupId,pondIdList);
        Map<String,Object> map = new HashMap<>();
        map.put("pondGroupId",pondGroupId);
        return BusinessDTO.sucessBusiness(map,"添加分组成功");
    }

    /**
     * 删除塘(棚)分组
     * @param pondGroupId
     * @return
     */
    @Transactional
    public BusinessDTO<Object> deletePondGroup(String pondGroupId){
        String companyId = AppSessionUtils.getCompanyId();
        PondGroup queryPondGroup = pondGroupMapper.selectById(pondGroupId);
        if(null == queryPondGroup){
            return BusinessDTO.failBusiness("分组不存在");
        }
        Integer type = queryPondGroup.getType();
        pondGroupMapper.delete(new QueryWrapper<PondGroup>().eq("id",pondGroupId).eq("company_id",companyId));
        this.deletePondGruopDetail(pondGroupId,type);
        return BusinessDTO.sucessBusiness(new HashMap<>(),"删除塘(棚)分组");
    }

    /**
     * 编辑塘(棚)分组
     * @param pondGroupId
     * @param groupName
     * @param pondIdList
     * @return
     */
    @Transactional
    public BusinessDTO<Object> editPondGroup(String pondGroupId,String groupName, List<String> pondIdList){
        log.info("=====editPondGroup.pondGroupId={},groupName={},pondIdList={}",pondGroupId,groupName,pondIdList);
        PondGroup queryPondGroup = pondGroupMapper.selectById(pondGroupId);
        if(null == queryPondGroup){
            return BusinessDTO.failBusiness("分组不存在");
        }
        Integer type = queryPondGroup.getType();
        if(StringUtils.isNotEmpty(groupName)){
            PondGroup pondGroup = new PondGroup();
            pondGroup.setId(pondGroupId);
            pondGroup.setGroupName(groupName);
            pondGroupMapper.updateById(pondGroup);
        }
        this.deletePondGruopDetail(pondGroupId,type);
        if(ObjectUtils.isNotEmpty(pondIdList)){
            BusinessDTO<Object> check = this.checkPondGroup(pondIdList,type);
            if( !check.isSucess()){
                throw new BusinessException(check.getMessage());
            }
        }
        this.addPondGroupDetail(pondGroupId,pondIdList);
        return BusinessDTO.sucessBusiness(new HashMap<>(),"编辑塘(棚)分组");
    }

    public  List<PondGroupDTO> queryPondIdByGroup(String companyId, List<String> groupIdList){
        return pondMapper.queryPondIdByGroup(companyId,groupIdList);
    }

    public List<String> queryPondIdByGroup(List<String> groupIdList){
        List<PondGroupDTO> pondGroupDTOS = this.queryPondIdByGroup(AppSessionUtils.getCompanyId(),groupIdList);
        List<String> retList = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(pondGroupDTOS)){
            for(PondGroupDTO dto :pondGroupDTOS){
                retList.add(dto.getPondId());
            }
        }
        return retList;
    }

    public PondEntity queryPondById(String pondId){
        PondEntity pond =  pondMapper.selectById(pondId);
        String companyId = AppSessionUtils.getCompanyId();
        if(null != pond && StringUtils.isNotEmpty(companyId) ){
            pond.setUserCompany(companyId.equals(pond.getCompanyId()));
        }
        CompanyEntity companyEntity = companyMapper.selectById(pond.getCompanyId());
        if(null != companyEntity){
            pond.setCompanyName(companyEntity.getName());
        }
        return pond;
    }

    public List<PondEntity> queryAllPondList(String companyId){
        return pondMapper.selectList(new QueryWrapper<PondEntity>().eq("company_id",companyId));
    }

    public List<PondEntity> queryAllPondList(){
        return this.queryAllPondList(AppSessionUtils.getCompanyId());
    }

    /**
     * 搜索塘列表
     * @param keywords
     * @return
     */
    public BusinessDTO<Object> searchPondList(String keywords){
        List<Map<String,Object>> pondList = pondMapper.searchPondList(AppSessionUtils.getCompanyId(),StringUtils.trimToEmpty(keywords));
        if(null == pondList){
            pondList = new ArrayList<>();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("pondList",pondList);
        return BusinessDTO.sucessBusiness(map,"搜索塘列表 ");
    }

    private BusinessDTO<Object> checkPondGroup(List<String> pondIdList,Integer type){
        CountPondAndGruopDetail countDTO = pondMapper.countPondAndGruopDetail(pondIdList,type);
        if(null == countDTO){
            return BusinessDTO.failBusiness("数据异常");
        }
        int pondCount = (null == countDTO.getPondCount())? -1:countDTO.getPondCount();
        int gruopDetailCount = (null == countDTO.getGroupDetailCount())? 0:countDTO.getGroupDetailCount();
        if( pondCount != pondIdList.size()){
            return BusinessDTO.failBusiness("包含不存在的塘");
        }
        if(gruopDetailCount>0){
            return BusinessDTO.failBusiness("塘已分其它组");
        }
        return BusinessDTO.sucessBusiness(null);
    }


}
